package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.banshengyanyu.bottomtrackviewlib.TrackModel;
import com.banshengyanyu.bottomtrackviewlib.interfaces.ClipVideoListener;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityVideoHandleBinding;
import stark.common.basic.utils.MediaUtil;

public class VideoHandleActivity extends BaseAc<ActivityVideoHandleBinding> implements ClipVideoListener {

    public static String sVideoPath;
    private long videoLength;
    private long tailorStartTime;
    private long tailorEndTime;

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.tvDuration.setText(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), getString(R.string.duration_style)) + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
                mDataBinding.sbProgress.setProgress(mDataBinding.videoView.getCurrentPosition());
            }
            mHandler.postDelayed(mTaskUpdateTime, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_handle;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoHandleBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.tvVideoHandleConfirm.setOnClickListener(this);
        mDataBinding.tvTime1.setOnClickListener(this);
        mDataBinding.tvTime2.setOnClickListener(this);
        mDataBinding.tvTime3.setOnClickListener(this);

        mHandler = new Handler();
        mDataBinding.sbProgress.setMax((int) MediaUtil.getDuration(sVideoPath));
        mDataBinding.tvDuration.setText("00:00" + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
        mDataBinding.videoView.setVideoPath(sVideoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvDuration.setText(TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)) + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
                mDataBinding.ivPlay.setSelected(false);
                mp.seekTo(1);
                stopTime();
            }
        });
        mDataBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mDataBinding.sbProgress.setMax(mp.getDuration());
                mDataBinding.sbProgress.setProgress(mp.getCurrentPosition());
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPlay://播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlay.setSelected(false);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlay.setSelected(true);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.tvTime1://3s
                clearSelection();
                mDataBinding.llTime1.setBackgroundColor(Color.parseColor("#0D4EFB"));
                mDataBinding.bottomTrackView.getClipVideoTrackView().clipSetClipDuration(3750);//750毫秒偏差
                break;
            case R.id.tvTime2://5s
                clearSelection();
                mDataBinding.llTime2.setBackgroundColor(Color.parseColor("#0D4EFB"));
                mDataBinding.bottomTrackView.getClipVideoTrackView().clipSetClipDuration(5750);
                break;
            case R.id.tvTime3://10s
                clearSelection();
                mDataBinding.llTime3.setBackgroundColor(Color.parseColor("#0D4EFB"));
                mDataBinding.bottomTrackView.getClipVideoTrackView().clipSetClipDuration(10750);
                break;
            case R.id.tvVideoHandleConfirm://确认裁剪
                cutVideo();
                break;
        }
    }

    @Override
    protected void initData() {
        videoLength = MediaUtil.getDuration(sVideoPath);
        //设置剪切功能
        mDataBinding.bottomTrackView.setClipVideoMode(TrackModel.ClipVideoMode.OPERATION);
        mDataBinding.bottomTrackView.setMode(TrackModel.ClipMode.CLIP);
        mDataBinding.bottomTrackView.initInfo(sVideoPath, videoLength, true,
                0, videoLength, 1.0f);
        mDataBinding.bottomTrackView.setClipVideoListener(this);
    }

    //剪切视频
    private void cutVideo() {
        if (tailorEndTime == 0 && tailorStartTime == 0) {
            ToastUtils.showShort(R.string.cut_tip);
        } else {
            EditorHolder.editor().videoCut(sVideoPath, tailorStartTime, tailorEndTime, new IEditorListener() {
                @Override
                public void onSuccess(String outFilePath) {
                    dismissDialog();
                    ToastUtils.showShort(R.string.cut_suc);
                    GifMakeActivity.sVideoPath = outFilePath;
                    startActivity(GifMakeActivity.class);
                }

                @Override
                public void onFailure(String errMsg) {
                    dismissDialog();
                    ToastUtils.showShort(R.string.cut_def);
                }

                @Override
                public void onProgress(int progress) {
                    showDialog(getString(R.string.corp_ing) + progress + "%");
                }
            });
        }
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTime();
    }


    @Override
    protected void onResume() {
        super.onResume();
        mDataBinding.videoView.seekTo(1);
    }

    @Override
    public void clipLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {
        tailorStartTime = cutStartTime;
        tailorEndTime = cutEndTime;
    }

    @Override
    public void clipRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {
        tailorStartTime = cutStartTime;
        tailorEndTime = cutEndTime;
    }

    @Override
    public void cropFirstLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropFirstRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropSecondLeftSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void cropSecondRightSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void splitFirstSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    @Override
    public void splitSecondSliderChange(long duration, long cutStartTime, long cutEndTime, long originTrimIn, long originTrimOut) {

    }

    private void clearSelection() {
        mDataBinding.llTime1.setBackgroundColor(Color.parseColor("#1D1F31"));
        mDataBinding.llTime2.setBackgroundColor(Color.parseColor("#1D1F31"));
        mDataBinding.llTime3.setBackgroundColor(Color.parseColor("#1D1F31"));
    }

}